package com.sinoglobal.sinostore;

import android.content.Intent;
import android.os.Bundle;

import com.umeng.socialize.bean.CallbackConfig.ICallbackListener;
import com.umeng.socialize.bean.RequestType;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * 
 * @author lgy
 * @createdate 2015-1-29 下午3:41:25
 * @Description:TODO (用一句话描述该类做什么) 有分享界面需要继承的基类 需要关联
 *                   social_sdk_library_project这个library
 */
public abstract class ShareAbstractActivity extends StateBaseActivity {
	// 首先在您的Activity中添加如下成员变量
	protected final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share", RequestType.SOCIAL);
	private ICallbackListener listener;
	private UMImage umImage;
	private QQShareContent qqShareContent;
	private QZoneShareContent qZoneShareContent;
	private WeiXinShareContent weixinContent;
	private CircleShareContent circleMedia;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		shareInfo();
	}

	/**
	 * 
	 * @author lgy
	 * @createdate 2015-1-29 下午3:23:10
	 * @Description: (用一句话描述该方法做什么) 分享面板打开和设置内容
	 * @param inviteContent
	 *            分享内容
	 * @param platform
	 *            分享平台 如果传null 使用友盟默认分享面板
	 * @param shareUrl
	 *            分享链接
	 * @param isIcon
	 *            资源文件id 如果没有传0 会分享picUrl
	 * 
	 *            TODO(如果isIcon值为0,picUrl一定要有图片地址)
	 * @param picUrl
	 *            网络图片
	 */
	public void setShare(String content, String title, SHARE_MEDIA platform, String shareUrl, int isIcon, String picUrl) {

		if (isIcon == 0) {
			umImage = new UMImage(ShareAbstractActivity.this, picUrl);

		} else {
			umImage = new UMImage(ShareAbstractActivity.this, isIcon);

		}
		// 设置分享内容
		mController.setShareContent(content);
		mController.setShareImage(umImage);

		qZoneShareContent.setTargetUrl(shareUrl);
		qZoneShareContent.setTitle(title);
		qZoneShareContent.setShareContent(content);
		qZoneShareContent.setShareImage(umImage);
		mController.setShareMedia(qZoneShareContent);

		qqShareContent.setTargetUrl(shareUrl);
		qqShareContent.setTitle(title);
		qqShareContent.setShareContent(content);
		qqShareContent.setShareImage(umImage);
		mController.setShareMedia(qqShareContent);

		// 设置微信好友内容
		weixinContent.setTargetUrl(shareUrl);
		weixinContent.setTitle(title);
		weixinContent.setShareContent(content);
		weixinContent.setShareImage(umImage);
		mController.setShareMedia(weixinContent);

		// 设置微信朋友圈内容
		circleMedia.setTargetUrl(shareUrl);
		circleMedia.setShareContent(title);
		circleMedia.setTitle(content);
		circleMedia.setShareImage(umImage);
		mController.setShareMedia(circleMedia);

		// 如果不是单个分享平台 使用友盟默认分享平台
		if (platform == null) {
			mController.registerListener(listener);
			try {
				mController.openShare(ShareAbstractActivity.this, false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			// 参数1为当前Activity对象， 参数2为要分享到的目标平台， 参数3为分享操作回调接口
			mController.postShare(ShareAbstractActivity.this, platform, new SnsPostListener() {

				@Override
				public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {
					if (arg1 == StatusCode.ST_CODE_SUCCESSED) {
						showShortToast(getString(R.string.share_ok));
					} else if (arg1 == 40000) {
					} else {
						showShortToast(getString(R.string.share_fail));
					}
				}

				@Override
				public void onStart() {
					showShortToast(getResources().getString(R.string.shareing));
				}

			});
		}
		// }
	}

	/**
	 * 
	 * @author lgy
	 * @createdate 2015-3-25 下午3:54:50
	 * @Description: 设置初始化
	 * 
	 */
	private void shareInfo() {
		mController.getConfig().setSsoHandler(new SinaSsoHandler()); // 如果新浪安装客户端,会走新浪客户端
		// 选择平台
		mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN, SHARE_MEDIA.SMS, SHARE_MEDIA.TENCENT);
		// 设置分享平台面板中显示的平台
		mController.getConfig().setPlatformOrder(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
				SHARE_MEDIA.QZONE);

		mController.getConfig().closeToast();// 关闭土司
		weixinContent = new WeiXinShareContent();
		circleMedia = new CircleShareContent();
		qZoneShareContent = new QZoneShareContent();
		qqShareContent = new QQShareContent();

		// TODO 需要替换新申请的QQ互联分享key
		String qqAppID = getString(R.string.qqAppID);
		String qqAppSecret = getString(R.string.qqAppSecret);
		// 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, qqAppID, qqAppSecret);
		qqSsoHandler.addToSocialSDK();

		// 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, qqAppID, qqAppSecret);
		qZoneSsoHandler.addToSocialSDK();

		// TODO 需要替换新申请的QQ互联分享key
		String wxAppID = getString(R.string.wxAppID);
		String wxAppSecret = getString(R.string.wxAppSecret);
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(this, wxAppID, wxAppSecret);
		wxHandler.addToSocialSDK();
		// 添加微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(this, wxAppID, wxAppSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

		listener = new SnsPostListener() {

			@Override
			public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {
				if (arg1 == StatusCode.ST_CODE_SUCCESSED) {
					showShortToast(getResources().getString(R.string.share_ok));
					// 判断平台 请求完成任务接口

				} else if (arg1 == 40000) {
				} else {
					showShortToast(getResources().getString(R.string.share_fail));
				}
			}

			@Override
			public void onStart() {
				showShortToast(getResources().getString(R.string.shareing));
			}

		};

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (listener != null) {
			mController.unregisterListener(listener);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}
}
